CSS Selectors

Comprehensive Explanation

CSS selectors are patterns used to select and style HTML elements. They are a fundamental part of CSS and allow you to apply styles to specific elements or groups of elements on a web page.

Types of CSS Selectors:

  1. Element Selector: Selects all elements of a specified type.
  2. Class Selector: Selects elements with a specific class attribute.
  3. ID Selector: Selects a single element with a specific id attribute.
  4. Universal Selector: Selects all elements on the page.
  5. Attribute Selector: Selects elements based on their attributes or attribute values.
  6. Descendant Selector: Selects elements that are descendants of another element.
  7. Child Selector: Selects elements that are direct children of another element.
  8. Adjacent Sibling Selector: Selects an element that comes immediately after another element.
  9. General Sibling Selector: Selects elements that are siblings of another element.
  10. Pseudo-class Selector: Selects elements based on a certain state or condition.
  11. Pseudo-element Selector: Selects and styles a part of an element.

Line-by-Line Code Examples


/* Element Selector */
p {
    color: blue;
}

/* Class Selector */
.highlight {
    background-color: yellow;
}

/* ID Selector */
#header {
    font-size: 24px;
}

/* Universal Selector */
* {
    margin: 0;
    padding: 0;
}

/* Attribute Selector */
input[type="text"] {
    border: 1px solid #ccc;
}

/* Descendant Selector */
article p {
    line-height: 1.6;
}

/* Child Selector */
ul > li {
    list-style-type: square;
}

/* Adjacent Sibling Selector */
h1 + p {
    font-weight: bold;
}

/* General Sibling Selector */
h1 ~ p {
    color: gray;
}

/* Pseudo-class Selector */
a:hover {
    text-decoration: underline;
}

/* Pseudo-element Selector */
p::first-line {
    font-variant: small-caps;
}
                

Line-by-Line Explanation

  1. p { color: blue; }: Selects all <p> elements and sets their text color to blue.
  2. .highlight { background-color: yellow; }: Selects elements with the class "highlight" and sets their background color to yellow.
  3. #header { font-size: 24px; }: Selects the element with the ID "header" and sets its font size to 24 pixels.
  4. * { margin: 0; padding: 0; }: Selects all elements and removes their default margin and padding.
  5. input[type="text"] { border: 1px solid #ccc; }: Selects input elements with the type attribute set to "text" and adds a light gray border.
  6. article p { line-height: 1.6; }: Selects <p> elements that are descendants of <article> elements and sets their line height.
  7. ul > li { list-style-type: square; }: Selects <li> elements that are direct children of <ul> elements and changes their bullet style to squares.
  8. h1 + p { font-weight: bold; }: Selects the first <p> element that comes immediately after an <h1> element and makes it bold.
  9. h1 ~ p { color: gray; }: Selects all <p> elements that are siblings of <h1> elements and sets their text color to gray.
  10. a:hover { text-decoration: underline; }: Adds an underline to links when hovered over.
  11. p::first-line { font-variant: small-caps; }: Selects the first line of all <p> elements and displays it in small caps.

Selector Specificity and Cascading

When multiple selectors target the same element, CSS uses specificity rules to determine which styles to apply:

  1. Inline styles (highest specificity)
  2. ID selectors
  3. Class selectors, attribute selectors, and pseudo-classes
  4. Element selectors and pseudo-elements (lowest specificity)

The cascade determines which styles take precedence when rules conflict. It considers:

  1. Importance (!important declaration)
  2. Specificity
  3. Source order (later rules override earlier ones)

Understanding selector specificity and the cascade is crucial for writing efficient and maintainable CSS code.

Best Practices for Using CSS Selectors

  • Use meaningful class names that describe the element's purpose or content.
  • Avoid overly specific selectors to improve code maintainability.
  • Utilize the cascade to your advantage by structuring your CSS from general to specific.
  • Group related selectors to reduce repetition and improve readability.
  • Use comments to explain complex selectors or their purpose in your stylesheets.
  • Consider using a naming convention like BEM (Block Element Modifier) for larger projects.
  • Be cautious with universal selectors (*) as they can impact performance on large sites.
  • Use ID selectors sparingly, as they have high specificity and can make overriding styles difficult.